﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Desktop.Controls
{
    /// <summary>
    /// Interaction logic for GlassButton.xaml
    /// </summary>
    public partial class ToolBarButton : UserControl, INotifyPropertyChanged
    {
        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        public static readonly RoutedEvent ClickEvent =
            EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble,
                typeof(RoutedEventHandler), typeof(ToolBarButton));

        [Category("Behavior")]
        public event RoutedEventHandler Click
        {
            add
            {
                AddHandler(ClickEvent, value);
            }
            remove
            {
                RemoveHandler(ClickEvent, value);
            }
        }

        #endregion

        #region Constructors

        public ToolBarButton()
        {
            InitializeComponent();
            primaryButton.DataContext = this;
            dropDownButton.DataContext = this;
        }

        #endregion

        #region Properties

        public ICommand Command
        {
            get
            {
                return (ICommand)GetValue(CommandProperty);
            }
            set
            {
                if (Command != value)
                {
                    SetValue(CommandProperty, value);
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Command"));
                    }
                }
            }
        }

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(ToolBarButton), new PropertyMetadata(null));

        public IInputElement CommandTarget
        {
            get
            {
                return (IInputElement)GetValue(CommandTargetProperty);
            }
            set
            {
                if (CommandTarget != value)
                {
                    SetValue(CommandTargetProperty, value);
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("CommandTarget"));
                    }
                }
            }
        }

        public static readonly DependencyProperty CommandTargetProperty =
            DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(ToolBarButton), new PropertyMetadata(null));

        public Orientation Orientation
        {
            get
            {
                return (Orientation)GetValue(OrientationProperty);
            }
            set
            {
                if (Orientation != value)
                {
                    SetValue(OrientationProperty, value);
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Orientation"));
                    }
                }
            }
        }

        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(ToolBarButton), new PropertyMetadata(Orientation.Vertical));

        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    value = string.Empty;
                }
                if (Text != value)
                {
                    SetValue(TextProperty, value);
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Text"));
                    }
                }
            }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(ToolBarButton), new PropertyMetadata(string.Empty));

        public ImageSource ImageSource
        {
            get
            {
                return (ImageSource)GetValue(ImageSourceProperty);
            }
            set
            {
                if (ImageSource != value)
                {
                    SetValue(ImageSourceProperty, value);
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("ImageSource"));
                    }
                }
            }
        }

        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ToolBarButton), new PropertyMetadata(null));

        public ContextMenu DropDownMenu
        {
            get
            {
                return (ContextMenu)GetValue(DropDownMenuProperty);
            }
            set
            {
                if (DropDownMenu != value)
                {
                    SetValue(DropDownMenuProperty, value);
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("DropDownMenu"));
                    }
                }
            }
        }

        public static readonly DependencyProperty DropDownMenuProperty =
            DependencyProperty.Register("DropDownMenu", typeof(ContextMenu), typeof(ToolBarButton), new PropertyMetadata(null));

        #endregion

        #region Event Handlers

        private void primaryButton_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(ToolBarButton.ClickEvent, this));
        }

        private void dropDownButton_Click(object sender, RoutedEventArgs e)
        {
            if ((DropDownMenu == null) || !DropDownMenu.HasItems)
            {
                e.Handled = true;
                return;
            }

            DropDownMenu.PlacementTarget = this;
            DropDownMenu.IsOpen = true;

            e.Handled = true;
        }

        #endregion
    }
}